package net.yxsoft.controller.pc.sysmgt.infrastructure;

import net.yxsoft.enumclass.StateEnumCls;
import net.yxsoft.enumclass.VisibleEnumCls;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.model.infrastructure.TsCode;
import net.yxsoft.model.infrastructure.TsMenu;
import net.yxsoft.service.infrastructure.MenuService;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * 菜单管理Controller
 * Created by Leo on 2016/5/14.
 */
public class MenuController extends AuthController {

  /**
   * Service
   */
  private static final MenuService menuService = MenuService.INSTANCE;

  /**
  * @MethodName: index
  * @Description: 主视图
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void index() throws IllegalAccessException {
    List<StateEnumCls> state = StateEnumCls.defaults(StateEnumCls.class);
    List<VisibleEnumCls> visible = VisibleEnumCls.defaults(VisibleEnumCls.class);

    setAttr("state", state);
    setAttr("visible", visible);

    render("menu.html");
  }

  //region 菜单管理

  /**
  * @MethodName: getMenuById
  * @Description: 根据菜单主键获取菜单信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void getMenuById() throws IllegalAccessException {
    renderJson(menuService.getMenuById(getPara("menuId")));
  }

  /**
  * @MethodName: getMenuType
  * @Description: 根据菜单类型返回枚举类
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void getMenuType() throws IllegalAccessException {
    renderJson(menuService.getMenuType(getPara("type"), true));
  }

  /**
  * @MethodName: getMenuTypeList
  * @Description: 根据父级菜单类型返回子级菜单类型可选集合
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void getMenuTypeList() {
    renderJson(menuService.getMenuTypeList(getPara("type")));
  }

  /**
  * @MethodName: saveMenu
  * @Description: 保存菜单信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void saveMenu() {
    renderJson(menuService.saveMenu(getModel(TsMenu.class, "", true), getLoginUser()));
  }

  /**
  * @MethodName: deleteMenuById
  * @Description: 根据主键删除菜单
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void deleteMenuById() throws SQLException {
    renderJson(menuService.deleteMenuById(getPara("menuId"), getLoginUser()));
  }

  /**
  * @MethodName: deleteMenus
  * @Description: 根据主键集合删除菜单
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void deleteMenus() throws SQLException {
    String[] menuIds = getPara("menuIds").split(",");
    renderJson(menuService.deleteMenus(menuIds, getLoginUser()));
  }

  /**
  * @MethodName: getMenusTree
  * @Description: 获取菜单树
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void getMenusTree() throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
    renderJson(menuService.getMenusTree());
  }

  /**
  * @MethodName: getMenuTypePicker
  * @Description: 获取菜单类型picker
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:29
  * @author jhf
  */
  public void getMenuTypePicker(){
      List<TsCode> list = new TsCode().dao().find("select * from TS_CODE where CODETYPE = 'MENUTYPE' and LEAF = '2'");
      renderJson(list);
  }
  //endregion
}
